Ext.define('AmpleHarvest.views.flatfile.main',
{
	extend			: 'Ext.panel.Panel',
	alias			: 'widget.ampleharvest-views-flatfile-main',
	requires		:
	[
	 	'AmpleHarvest.util.utility'
	],
	bubbleEvents	:
	[

	],
	anchor			: '100% 100%',
	layout			: 'border',
	// PASSED VARIABLES
	m_str_id		: false,	
	
	// MEMBER VARIABLES
//	m_str_location	: '/tpds_services_test/',
//	m_str_path		: 'procurement-rest/procurement/ext/ge/{id}',
	
	m_str_location	: '/tpds_services/',
	m_str_path		: 'article-rest-cb/site/ext/articles/{id}',
//	
	m_obj_map	: 
			{
         	   	M_STR_CONTENT : 
 	   				{
 		   				serviceID 	: 'content',
 		   				ah_obj		: 'AmpleHarvest.objects.content.content',
 		   				alias		: 'ampleharvest-objects-content-content',
 		   				location	: 'center',
 		   				childof		: {
 		   					ah_obj	: 'AmpleHarvest.views.flatfile.center',
 		   					alias	: 'ampleharvest-views-flatfile-center',
 		   					location: 'center',
 		   					settitle: true
 		   				}
 	   				},
         	   	M_OBJ_METADATA :
         	   		{
 		   				serviceID 	: 'metadata',
 		   				ah_obj		: 'AmpleHarvest.objects.harvestMetadata.harvestdata',
 		   				location	: 'south',
 		   				childof		: {
		   					ah_obj	: 'AmpleHarvest.views.flatfile.center',
		   					alias	: 'ampleharvest-views-flatfile-center',
		   					location: 'center',
		   				}
         	   		},
         	   	M_OBJ_HIERARCHY	:
         	   		{
		         	   	serviceID 	: 'hierarchy',
		   				ah_obj		: 'AmpleHarvest.objects.hierarchy.hierarchy',
		   				childof		: {
		   					ah_obj	: 'AmpleHarvest.views.flatfile.west',
		   					alias	: 'ampleharvest-views-flatfile-west',
		   					location: 'west',
		   				}
         	   		},
         	   	M_OBJ_CCE	:
         	   		{
		         	   	serviceID 	: 'cce',
		   				ah_obj		: 'AmpleHarvest.objects.cce.cce',
		   				childof		: {
		   					ah_obj	: 'AmpleHarvest.views.flatfile.east',
		   					alias	: 'ampleharvest-views-flatfile-east',
		   					location: 'east'
		   				}
         	   		}
         	},


	listeners		:
	{
		dataResponse	: function(obj_data)
		{
			console.log(obj_data);
			
			if(Ext.isString(obj_data.content))
			{
				obj_data = this.old_data_convert(obj_data)
			}
			
			var obj_data = obj_data.data;
			var obj_linkTree = obj_data.linkTree.link;
			
			this.m_str_title = obj_data.title+'::'+this.m_str_id;
			
			m_obj_AmpleHarvest_ViewPort.fireEvent(AmpleHarvest.eventHandler.cWindowEvents.C_STR_EVENT_ADD_ACTION,
					'AmpleHarvest.views.flatfile.main',
					'FLATFILE :'+this.m_str_title,
					{
						m_str_id : this.m_str_id
					}
				);
			
			this.obj_items = {};
			
			for(var key in this.m_obj_map)
			{
				var str_urn = this.m_obj_tpdsrestutil.getLink(this.m_obj_map[key]['serviceID'], obj_linkTree, 'rel', 'urn');
				if(str_urn)
				{
					if(key == 'M_STR_CONTENT')
					{
						this.createFrameObject(key)
					}

					this.obj_items[key] = Ext.create(this.m_obj_map[key]['ah_obj'],
							{
								AHcaller			: this,
								m_str_data_source 	: this.m_str_location+str_urn,
								m_str_keyID			: key,
								region				: this.m_obj_map[key]['location']
							});
				}
			}
		},	
		
		objectResponse	: function(str_keyID)
		{
			this.createFrameObject(str_keyID);
			
			var obj_panel = this.down(this.m_obj_map[str_keyID]['childof']['alias']);
			
			if(obj_panel.collapsed)
			{
				obj_panel.expand(false);
			}
			obj_panel.add(this.obj_items[str_keyID]);
		},
		
		beforedestory	: function(obj_self)
		{
			
			obj_self.m_obj_util.destroy();
			obj_self.m_obj_tpdsrestutil.destroy();
		},
		
		downloadFile	: function(str_dl_path)
		{
			window.open(this.m_str_location+str_dl_path,"_ ");
		},
		
		genParentLink	: function(str_parent_path)
		{
			var str_stripped_id = str_parent_path.split('/').pop();

			var obj_parent_button = Ext.create('Ext.button.Button',
					{
						text	: 'Open Parent',
						handler	: function()
						{
							m_obj_AmpleHarvest_ViewPort.fireEvent(AmpleHarvest.eventHandler.cWindowEvents.C_STR_EVENT_OPEN_FLATFILE,
										str_stripped_id
									);
						}
					});
			
			var obj_toolbar = this.down('toolbar').add(obj_parent_button);
		},
		
		genDownloadLink : function(str_dl_path)
		{
			var obj_download_button = Ext.create('Ext.button.Button',
					{
						text	: 'Download File',
						scope	: this,
						handler	: function()
						{
							this.fireEvent('downloadfile',
										str_dl_path
									);
						}
					});
			var obj_toolbar = this.down('toolbar').add(obj_download_button);
		},
		
	     maximizeContent : function()
	     {
	    	 this.items.each(
	 	    		function()
	 	    		{
	 	    			if(this.region != 'center')
	 	    			{ 
	 	    				try{this.placeholderFor.collapse(); }catch(e){};
	 	    			}
	 	    		}
	 	    	 );
	     },
	     
	     contractContent : function()
	     {
	    	 this.items.each(
	    		function()
	    		{
	    			if(this.region != 'center')
	    			{ 
	    				try{this.placeholderFor.expand(); }catch(e){};
	    			}
	    		}
	    	 );
	     },
	     
	     printContent : function()
	     {
	    	
	     },
	     
	     highlight : function(str_id, str_class, str_attribute)
	     {
    	 
	    	 if (!str_attribute)
				{
	    		 str_attribute = 'id';
				}

			var DOM_Object = this.down(this.m_obj_map.M_STR_CONTENT.alias).getEl().select('element[' + str_attribute
					+ '=' + str_id + ']');
			console.log(DOM_Object);
			DOM_Object.set(
					{
						'class'	: str_class
					}, true);
	     }
	},
	
	initComponent	: function()
	{		
		console.log(this.m_str_id);
		// TODO Direct linking to flatfile/main does not know what service to use fix that
		if(Ext.isObject(this.obj_config))
		{
			//TODO remove obj_form_info from the event log and just send in obj_params in the do_AddAction of cWindowEvents
			this.m_str_id = this.obj_config.m_str_id;
		}
		if(this.m_str_id||this.m_str_urn)
		{
			//REMOVE ME hardcoded ID to override the old IDs in solr
			//this.m_str_id = 'market-structure.pdf';
			
			var str_unique_id = (((1+Math.random())*0x10000)|0).toString(16).substring(1);
			this.m_obj_util = Ext.create('AmpleHarvest.util.utility');
			this.m_obj_util.AHcaller = this;
			this.m_obj_tpdsrestutil = Ext.create('AmpleHarvest.util.tpdsrestutil');
			this.m_str_path = !Ext.isString(this.m_str_urn) ? this.m_str_location+this.m_str_path.replace('{id}', this.m_str_id) : this.m_str_location+this.m_str_urn;
			
			this.m_obj_util.getData(this.m_str_path, {},{},'GET', {}, {});
		} else {
			this.html = "THIS PAGE REQUIRES AN ID as m_str_id";
		}

		this.callParent();
		
	},
	
	createFrameObject		: function(str_key)
	{
		if(!Ext.isObject(this.down(this.m_obj_map[str_key]['childof']['alias'])))
		{
			var obj_section = Ext.create(this.m_obj_map[str_key]['childof']['ah_obj'],
						{
							region	: this.m_obj_map[str_key]['childof']['location']
						});
			if(this.m_obj_map[str_key]['childof']['settitle'])
			{
				obj_section.setTitle(this.m_str_title);
			}
			
			this.add(obj_section);
		}
	},
	
	//TODO this will no longer be needed once the rest/data/data.php service is not used to pull json from amplesilo
	old_data_convert	: function(obj_data)
	{
		var obj_identity = new Object();
		
		
		
		return obj_identity; 
	}
});


